﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    class SubjectProjectAttenActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var refer_id = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Id", g =>
           {
               return new Web.UITextDialog() { Title = "关注Id" };
           }), 0);
            var user = this.Context.Token.Identity();
            if (user.IsAuthenticated == false)
            {
                
                this.Context.Send("Login",true);
                // response.Redirect("Account", "Login");
            }

            var section = this.AsyncDialog("section", g => this.DialogValue("1"));
            var row = this.AsyncDialog("row", g => this.DialogValue("1"));
            var ui = this.AsyncDialog("UI", g => this.DialogValue("none"));

            var vale = new UMC.Web.WebMeta().Put("section", section).Put("row", row).Put("method", "VALUE").Put("reloadSinle", true);


            var member = DataFactory.Instance().ProjectMember(refer_id, user.Id.Value);
            if (member != null)
            {
                switch (member.AuthType ?? WebAuthType.All)
                {
                    default:
                    case WebAuthType.Guest:
                        DataFactory.Instance().Delete(new ProjectMember { user_id = user.Id, project_id = refer_id });
                        break;
                    case WebAuthType.User:
                        this.AsyncDialog("Confrm", g => new UIConfirmDialog("你是项目的专栏作家，要取消此项目关注吗"));
                        DataFactory.Instance().Delete(new ProjectMember { user_id = user.Id, project_id = refer_id });
                        break;
                    case WebAuthType.Admin:
                        if (DataFactory.Instance().ProjectMember(refer_id, WebAuthType.Admin).Length == 1)
                        {
                            this.Prompt("你是最后一个项目主，不能退出此项目");
                        }
                        else
                        {
                            this.AsyncDialog("Confrm", g => new UIConfirmDialog("你是项目的项目主，要取消此项目关注吗"));
                            DataFactory.Instance().Delete(new ProjectMember { user_id = user.Id, project_id = refer_id });
                        }
                        break;
                }

                this.Context.UIEvent("UI.Edit", ui, vale.Put("value", new UMC.Web.WebMeta().Put("button", "+关注").Put("button-color", "#e67979").Put("desc", String.Format("{0}人", 1))), true);
            }
            else
            {

                DataFactory.Instance().Put(new ProjectMember
                {
                    project_id = refer_id,
                    user_id = user.Id,
                    CreationTime = DateTime.Now,
                    AuthType = WebAuthType.Guest
                });

                DataFactory.Instance().Put(new ProjectDynamic
                {
                    Time = UMC.Data.Utility.TimeSpan(DateTime.Now),
                    user_id = user.Id,
                    Explain = "加入了项目",
                    project_id = refer_id,
                    Alias = user.Alias,
                    refer_id = 0,
                    Title = "项目成员",
                    Type = DynamicType.Member
                });

                this.Context.UIEvent("UI.Edit", ui, vale.Put("value", new UMC.Web.WebMeta().Put("button", "已关注").Put("button-color", "#25b864").Put("desc", String.Format("{0}人", +1))), true);

            }




        }

    }
}